Skip to content

fix: propagate the API run id to the tracker - #1327

Open
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/api-run-id-never-propagates
Open

fix: propagate the API run id to the tracker#1327
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/api-run-id-never-propagates

Conversation

@davidberenstein1957

Copy link
Copy Markdown
Collaborator

What

EmissionsTracker.run_id is now a property backed by a locally generated uuid that returns the API run id as soon as one exists, and the API run is created in start() rather than on the first emission.

Why

With OutputMethod.API enabled, the tracker read run_id from CodeCarbonAPIOutput at construction time, but that handler builds its ApiClient with create_run_automatically=False and only creates the run lazily in _ensure_api_run. Nothing ever wrote the resulting id back, so tracker.run_id stayed None and _prepare_emissions_data recorded the literal string "None" in every row.

Consequences for users: CSV data could not be joined with the API data, on_csv_write="update" matched on "None" and overwrote the previous run's row, and per-task / BOAMPS filenames all collided on ..._None.csv.

Creating the run in start() (instead of only fixing the property) makes sure a single run has a single id everywhere, including rows persisted before the first API call. Failures there are logged and fall back to the local uuid, so an unreachable API no longer prevents tracking.

Verified

tests/test_emissions_tracker.py::TestCarbonTracker::test_run_id_with_api_output_is_never_none asserts tracker.run_id is not None and that the run id written to emissions.csv matches the API run id. It fails on master and passes with this change.

uv run pytest tests/test_emissions_tracker.py tests/output_methods tests/test_tracking_inference.py tests/test_api_call.py -q passes locally.

Closes #1303

🤖 Generated with Claude Code

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.50%. Comparing base (3ec31a0) to head (0221c73).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1327      +/-   ##
==========================================
+ Coverage   91.43%   91.50%   +0.06%     
==========================================
  Files          49       49              
  Lines        5057     5063       +6     
==========================================
+ Hits         4624     4633       +9     
+ Misses        433      430       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@davidberenstein1957
davidberenstein1957 marked this pull request as ready for review August 12, 2026 19:14
@davidberenstein1957
davidberenstein1957 requested a review from a team as a code owner August 12, 2026 19:14
The tracker generated its own run_id and never picked up the one the API
created, so API-backed runs persisted a local uuid (or the string "None")
instead of the run id the server knows about.

Create the API run in start(), where the output handler is already wired,
and adopt its run id when there is one. run_id stays a plain writable
attribute.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@davidberenstein1957
davidberenstein1957 force-pushed the fix/api-run-id-never-propagates branch from 08cdabf to 0221c73 Compare August 19, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tracker.run_id stays None when OutputMethod.API is enabled, CSV rows record run_id="None"

1 participant